package com.shinvent.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shinvent.common.GeneralFunctions;
import com.shinvent.common.Logger;
import com.shinvent.dao.BaseDao;
import com.shinvent.dao.T_INVENTORYDao;

public class Insert extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Insert() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("plain/text");
	    PrintWriter out = response.getWriter();
		T_INVENTORYDao inventoryDao = null;
		
		try{
			String keyValue = GeneralFunctions.getNextOrderID();
			String localOrderID = request.getParameter("local");
			String itemName = request.getParameter("itemName");
			String price = request.getParameter("price");
			String vendorID = request.getParameter("vid");
			String releaseMonth = request.getParameter("releaseMonth");
			String releaseYear = request.getParameter("releaseYear");
			String status = request.getParameter("status");
			
			inventoryDao = new T_INVENTORYDao();
			if(inventoryDao.insert(keyValue)){
				inventoryDao.update("LOCAL_ORDER_ID", localOrderID, keyValue);
				inventoryDao.update("ITEM_NAME", itemName, keyValue);
				inventoryDao.update("PRICE", price, keyValue);
				inventoryDao.update("VENDOR_ID", vendorID, keyValue);
				inventoryDao.update("MONTH_ID", releaseMonth, keyValue);
				inventoryDao.update("YEAR_ID", releaseYear, keyValue);
				inventoryDao.update("STATUS_ID", status, keyValue);
				out.print("OK|"+keyValue);
			}else{
				out.print("ERROR");
			}
			release(inventoryDao);
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			out.print("ERROR");
		}finally{
			release(inventoryDao);
		}
	}
	
	private void release(BaseDao dao){
		try{
			dao.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
